package services;

import dao.ArticleInfoDao;
import utils.ResultUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/10
 * Time:21:04
 **/
@WebServlet("/upart")
public class UpArtServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int succ = -1;
        String msg = "";
        int id = Integer.parseInt(req.getParameter("id"));
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if(id>0 && title != null && content != null && !title.equals("") && !content.equals("")){
            ArticleInfoDao articleInfoDao = new ArticleInfoDao();
            try {
                succ = articleInfoDao.upArt(id,title,content);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }else{
            msg = "无效的参数！";
        }
        HashMap<String,Object> res = new HashMap<>();
        res.put("succ",succ);
        res.put("msg",msg);
        ResultUtils.write(resp,res);
    }
}
